<?php

namespace app\common\model\mysql;

use think\Model;

class SOrderGoods extends Model
{
    //批量插入订单与订单商品关联表数据
    public function insertAllOrderGoods($orderGoodsData){
        if(empty($orderGoodsData)){
            return false;
        }
        return $this->insertAll($orderGoodsData);
    }

    //获取订单商品数据
    public function getOrderGoods($orderId,$field = '*'){
        $order = [
            "order_goods_id" =>"asc",
        ];
        $where = [
            ['order_id', '=', $orderId]
        ];
        $result = $this->where($where)->order($order)->field($field)->select();
        return $result->toArray();
    }

    //获取该订单是否有退款中订单
    public function getOrderGoodsStatus($orderId,$field = '*'){
        $where = [
            ['order_id', '=', $orderId],
            ['is_refund','=',1],
        ];
        $result = $this->where($where)->field($field)->find();

        return $result ? $result->toArray() : [];
    }

    //根据订单id修改订单商品表数据
    public function saveOrderGoodsId($orderId,$data){
        if(empty($orderId)){
            return false;
        }
        $where = [
            ['order_id', '=', $orderId],
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }

    //根据订单商品id修改订单商品表数据
    public function saveOrderGoodsbyId($orderGoodsId,$data){
        if(empty($orderGoodsId)){
            return false;
        }
        $where = [
            ['order_goods_id', '=', $orderGoodsId],
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }

    //查询支付订单商品购买总数
    public function getOrderGoodsCount($where){
        $result = $this->where($where)->sum("goods_num");
        return $result;
    }
}
